//index.js
//获取应用实例
import common from '../../utils/common.js'
const app = getApp()
const cateArr =  [
  {
    "id": 39611,
    "label": "轻奢"
    },
  {
    "id": 39614,
    "label": "现代"
    },
  {
    "id": 39615,
    "label": "中式"
    },
  {
    "id": 39616,
    "label": "新中式"
    },
  {
    "id": 39613,
    "label": "欧式"
    },
  {
    "id": 39612,
    "label": "美式"
    },
  {
    "id": 39608,
    "label": "北欧"
    }
]

Page({
  data: {
    cate:cateArr,
    openid: '44vpis+4qJhXwj3IXOgY',
    id: 39611,
    styleName: '轻奢',
    imageArr: [],
    imgheights: [],
    first:0,
    isRuleTrue:false,
    imgwidth: 750,
    current:0
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  sq: function () {
    var image = this.data.imageArr[this.data.current]
    var imageUrl = 'https://www.d-shang.com/' + image['source']
    common.sq(imageUrl)
  },
  imageLoad: function (e){
    //获取图片真实宽度
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比
      ratio = imgwidth / imgheight;
    //计算的高度值
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight
    var imgheights = this.data.imgheights
    //把每一张图片的高度记录到数组里
    imgheights[e.target.dataset['index']] = imgheight;// 改了这里 赋值给当前 index
    this.setData({
      imgheights: imgheights,
    })
  },
  bindchange: function (e) {
    this.setData({
      current: e.detail.current
    })
  },
  onLoad: function() {
    this.getImageById()
  },
  
  handleClick: function(e) {
    var value = e.currentTarget.dataset.id
    this.setData({
      id:value.id,
      styleName:value.label,
      first:0
    })
    this.hideRule()
    this.getImageById()
  },
  getPhoneNumber: function (e) {
    if (e.detail.errMsg == 'getPhoneNumber:fail user deny') {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '未授权',
        success: function (res) { }
      })
    } else {
      wx.showModal({
        title: '提示',
        showCancel: false,
        content: '同意授权',
        success: function (res) {
          console.log(e.detail.encryptedData)
         }
      })
    }},
  getImageById: function (){
    var that=this
    var url = 'https://www.d-shang.com/index.php?app/getnewsconbyid/?id=' + this.data.id + '&openid=' + this.data.openid;
    wx.request({
      url: url,// 仅为示例，并非真实的接口地址
      success(res) {
        var d = res.data.data
        that.setData({
          imageArr: d.module
        })
      }
    })
  },
  filter: function(){
    this.setData({
      isRuleTrue:true
    })
  },
  hideRule:function(){
    this.setData({
      isRuleTrue: false
    })
  },
  onShareAppMessage: function (ops) {
    if (ops.from === 'button') {
      // 来自页面内转发按钮
      console.log(ops.target)
    }
    return {
      title: '顶上整装定制',
      path: 'pages/index/index',
      success: function (res) {
        // 转发成功
        console.log("转发成功:" + JSON.stringify(res));
      },
      fail: function (res) {
        // 转发失败
        console.log("转发失败:" + JSON.stringify(res));
      }
    }

  }
})